#pragma once
#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>
#include <fstream>
#include "Log.hpp"


const std::string mydict = "./recourse/dict.txt";

class TransLate
{
public:
    TransLate(std::string dict_patch = mydict):_dict_path(dict_patch)
    {
        LoadDict();
        Parse();
    }
    void LoadDict()
    {
        std::ifstream in(_dict_path);
        std::string line;
        while (std::getline(in,line))
        {
            /* code */
            _lines.push_back(line);
        }
        
        in.close();
        lg.LogMessage(Debug,"Load successful,path:%s\n",_dict_path.c_str());
    }
    void debug()
    {
        for(auto &line:_lines)
        {
            std::cout << line <<std::endl;
        }
        for(auto& e : _dict)
        {
            std::cout << e.first << ":" << e.second<<std::endl;
        }
    }
    void Parse()
    {
        for(auto &line:_lines)
        {
            auto pos  = line.find(" ");
            if(pos == std::string::npos) continue;
            else
            {
                std::string word = line.substr(0,pos);
                std::string chinese = line.substr(pos + 1,std::string::npos);
                _dict.insert({word,chinese});
            }
        }
    }
    std::string Excute(const std::string &word)
    {
        auto iter = _dict.find(word);
        if(iter == _dict.end()) return "unknow";
        else return _dict[word];
    }
    ~TransLate()
    {}
private:
    std::string _dict_path;
    std::unordered_map<std::string,std::string> _dict;
    std::vector<std::string> _lines;
};

